<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>20.案例_分类导航</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        a {
            text-decoration: none;
            color: #333;
        }

        ul {
            list-style: none;
        }

        .container {
            width: 980px;
            margin: 0 auto;
        }

        .top {
            display: flex;
            width: 100%;
            height: 80px;
            align-items: center;
            justify-content: space-around;
            position: relative;
        }

        .top>li {
            text-align: center;
        }

        .top>li>a {
            border-bottom: 1px solid transparent;
        }

        .top>li:hover>a {
            border-color: #27ba9b;
            color: #27ba9b;
        }

        .top>li:hover>.sub {
            opacity: 1;
            transform: scale(1, 1);
        }

        .sub {
            position: absolute;
            left: 0;
            top: 80px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            display: flex;
            align-items: center;
            height: 100px;
            opacity: 0;
            transform: scale(1, 0);
            transform-origin: top center;
            transition: all 0.3s .1s;
        }

        .sub li {
            width: 120px;
            text-align: center;
        }

        .sub li a {
            display: block;
            font-size: 14px;
            padding-top: 8px;
        }

        .sub li a span {
            display: block;
        }

        .sub li a img {
            width: 60px;
            height: 60px;
            vertical-align: middle;
        }
    </style>
</head>

<body>
    <div class="container">
        <ul class="top" id="top">
            <!-- <li>
                <a href="javascript:;">首页</a>
                <ul class="sub">
                    <li>
                        <a href="javascript:;">
                        <span>砂锅厨具</span>
                        <img src="https://yanxuan.nosdn.127.net/3102b963e7a3c74b9d2ae90e4380da65.png?quality=95&imageView" alt="">
                        </a>
                    </li>
                </ul>
            </li> -->
        </ul>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/axios@0.27.2/dist/axios.min.js"></script>
    <script>
        axios.defaults.baseURL = 'http://ajax-api.itheima.net'
        axios.get('/api/category/top').then(res => {
            const arr = res.data.data
            // promise数组
            const arrAjax = arr.map(item => axios.get('/api/category/sub?id='+item.id))
            // console.log(arrAjax)
            Promise.all(arrAjax).then(res=>{
                console.log(res)
                let str = res.map(obj=>{
                    const p = obj.data.data
                    // 拼接二级菜单
                    const lis = p.children.map(subitem=>{
                        return `<li>
                            <a href="javascript:;">
                            <span>${subitem.name}</span>
                            <img src="${subitem.picture}" alt="">
                            </a>
                        </li>`
                    }).join('')
                    // 拼接一级菜单
                    return `
                        <li>
                            <a href="javascript:;">${p.name}</a>
                            <ul class="sub">
                                ${lis}
                            </ul>
                        </li>
                    `
                }).join('')

                //  console.log(str)
                document.querySelector('#top').innerHTML = str
            })

            // axios.get('/api/category/sub?id='+arr[0].id)
            // axios.get('/api/category/sub?id='+arr[1].id)
            // axios.get('/api/category/sub?id='+arr[2].id)
            // axios.get('/api/category/sub?id='+arr[3].id)
            // axios.get('/api/category/sub?id='+arr[4].id)
            // axios.get('/api/category/sub?id='+arr[5].id)
            // axios.get('/api/category/sub?id='+arr[6].id)
            // axios.get('/api/category/sub?id='+arr[7].id)
            // axios.get('/api/category/sub?id='+arr[8].id)
     
            // axios.get('/api/category/sub', { params: {id: res.data.data[0].id}})
            // axios({url: '/api/category/sub', params: {id: res.data.data[0].id}})
        })
    </script>
</body>

</html>